/*
 * ChildProcess.h
 *
 *  Created on:  May 30, 2009
 *      Author:  Zach Riggle A37627537
 *      Project: CSE410 Project 1
 */

#ifndef CHILDPROCESS_H_
#define CHILDPROCESS_H_

#include <iostream>
#include <string>
#include "Log.h"

using namespace std;

#define safeClose(x) if(x!=0&&x!=1){debug("Closing pipe " << x);close(x);}

/**
 * Child Process class.  Used to instantiate any process that is not the
 * main process.  Provides facilities to retrieve information about the
 * process, as well as see if it is alive.
 */
class ChildProcess {
public:
    const static int STILL_ALIVE = INT_MIN;

    ChildProcess();
    ChildProcess(int *io, int *oldfd, int *fd, string command, char **args);
    virtual ~ChildProcess();

    /**
     * @return The process's PID
     */
    int getPid();

    /**
     * Waits for the process to exit.
     */
    void wait();

    /**
     * @return The process's return value, or STILL_ALIVE if it has not
     * exited.
     */
    int getReturnValue();

    /**
     * @return The signal that stopped the process, or STILL_ALIVE if it
     * has not exited.
     */
    int getSignal();

    /**
     * @return True if the process is still alive, false if it has exited.
     */
    bool isAlive();

    /**
     * @return Human-readable name corresponding to a signal ID.
     */
    static string getSigName(int sigID);
protected:

    void initChildProcess();
    pid_t pid;
    int stat;
    bool hasExited;
    int _in;
    int _out;
};

#endif /* CHILDPROCESS_H_ */
